from PySide6.QtUiTools import QUiLoader
from PySide6.QtWidgets import QMessageBox

from entity.student import Student
from opsql import studentOp
# from entity.reader import Reader
# from opsql import readerOp


class Register():
    def __init__(self):
        self.ui = QUiLoader().load('view/注册.ui')
        self.ui.registerBtn.clicked.connect(self.register)

    def register(self):
        try:
            self.snum=int(self.ui.snumIn.text())
        except ValueError:
            QMessageBox.warning(self.ui, '警告', '学号必须为整数')
            return
        self.name = self.ui.nameIn.text()
        # name=readerOp.searchName()
        # if self.name in name:
        #     QMessageBox.warning(self.ui, '警告', '该用户名已存在')
        #     return
        self.password = self.ui.passwordIn.text()
        self.phone = self.ui.phoneIn.text()
        if not (self.snum and self.name and self.password and self.phone):
            QMessageBox.warning(self.ui, '警告', '请填写完整信息')
            return
        student=Student(self.snum,self.name,self.password,self.phone,0)
        # reader=Reader(None,self.name,self.phoneIn,None,1,self.snum,self.password)

        # if readerOp.add(reader):
        #     QMessageBox.information(self.ui, '提示', '注册成功')
        #     self.ui.close()
        # else:
        #     QMessageBox.warning(self.ui, '警告', '注册失败')
        if studentOp.add(student):
            QMessageBox.information(self.ui, '提示', '注册成功')
            self.ui.close()
        else:
            QMessageBox.warning(self.ui, '警告', '注册失败')